home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 101-125 / scopedisk122 / bassub / graphics.sub < prev    next >
Text File  |  1995-03-19  |  3KB  |  136 lines

  1. 'Small library of graphic functions which may be useful in Basic
  2. 'programs.
  3.  
  4. REM FNx
  5. 'converts x coordinates to pixel coordinates
  6. DEF FNx(x)=INT((x-xmin)+dx/2)/dx)
  7. REM FNy
  8. 'converts y coordinates to pixel coordinates
  9. DEF FNy(y)=(Vertical.pixels-5)-INT((y-ymin)+dy/2)/dy)
  10. REM FNmin
  11. 'finds minimum of 2 values
  12. DEF FNmin(x,y)
  13.     IF x < y THEN
  14.         FNmin = x
  15.     ELSE
  16.         FNmin = y
  17.     END IF
  18. END DEF
  19. REM FNmax
  20. 'finds maximum of 2 values
  21. DEF FNmax(x,y)
  22.     IF x > y THEN
  23.         FNmax = x
  24.     ELSE
  25.         FNmax = y
  26.     END IF
  27. END DEF
  28. REM AskWindow
  29. 'gets x1,y1,x2,y2 values for current window
  30. SUB AskWindow(x1%,x2%,y1%,y2%)
  31.     rp&=WINDOW(7)
  32.     x1%=PEEKW(rp&+4)
  33.     y1%=PEEKW(rp&+6)
  34.     x2%=WINDOW(2)-x1%
  35.     y2%=WINDOW(3)-y1%
  36. END SUB
  37. REM frame
  38. 'draws a frame in the current window
  39. SUB frame
  40.     CALL AskWindow( x1%, x2%, y1%, y2%)
  41.     LINE (x1%+64,y1%+1)-(x2%,y2%-10),,b
  42. END SUB
  43. REM axes
  44. 'draws x and y axes in current window
  45. SUB axes
  46.     CALL AskWindow(x1%,x2%,y1%,y2%)
  47.     LINE (x1%+64,y2%-10)-(x2%,y2%-10)
  48.     LINE (x1%+64,y1%+1)-(x1%+64,y2%-10)
  49. END SUB
  50. REM ticks
  51. 'draws x and y axes in current window and places tick marks at x% and y%
  52. 'intervals on x and y axes, respectively
  53. SUB ticks(x%,y%)
  54.     CALL AskWindow( x1%, x2%, y1%, y2%)
  55.     LINE (x1%+64,y2%-10)-(x2%,y2%-10)
  56.     LINE (x1%+64,y1%+1)-(x1%+64,y2%-10)
  57.     CALL mark(x1%,x2%,x%,1,y2%)
  58.     CALL mark(y1%,y2%,y%,2,x1%)
  59. END SUB
  60. REM mark
  61. 'used by ticks subroutine to place tick marks on axes
  62. SUB mark(u1%,u2%,us%,c%,l%)
  63.     IF u1%=0 then EXIT SUB
  64.     FOR u% = u1% to u2% step sgn(u2%-u1%)*us%
  65.         IF c%=1 then
  66.             LINE (u%,l%-3)-(u%,l%+3)
  67.         ELSE
  68.             LINE (l%-5,u%)-(l%+5,u%)
  69.         END IF
  70.     NEXT u%
  71. END SUB
  72. REM Bars
  73. 'Number of colors used is equal to number of elements in array d()
  74. '  so be sure you use a screen appropriately defined
  75. 'Make bar graph of data in array d()
  76. 'd() array of data elements
  77. ' n% is number of elements in d()
  78. SUB Bars(d(1),n%)
  79.     j%=FNmin(n%,14)-1
  80.     FOR i%=0 to j%
  81.         x%=100+i%*37
  82.         LINE (x%,150-d(i%))-STEP(10,-10),i%+1
  83.         LINE -STEP (0,d(i%)),i%+1
  84.         LINE -STEP (-10,10),i%+1
  85.         LINE -STEP (-20,-d(i%)),i%+1,bf
  86.         LINE -STEP (10,-10),i%+1
  87.         LINE -STEP (20,0),i%+1
  88.     NEXT i%
  89. END SUB
  90. REM PieChart
  91. 'Number of colors used is equal to number of elements in array value()
  92. '  so be sure you use a screen appropriately defined
  93. 'Draw Pie Chart of values from array value()
  94. 'value() array of data to plot
  95. 'n% number of elements in array value()
  96. SUB PieChart(value(1),n%)
  97.     pi=3.141529
  98.     f=.3
  99.     REDIM colr(n%)
  100.     FOR i%=1 TO n%
  101.         sum=sum+value(i%)
  102.         colr(i%)=i%+3
  103.     NEXT i%
  104.     mx=WINDOW(2)/2
  105.     my=WINDOW(3)/2
  106.     w1=0
  107.     radius=mx-100
  108.     CIRCLE (mx,my+20),radius,1,pi,2*pi,f
  109.     LINE (mx-radius,my)-(mx-radius,my+20)
  110.     LINE (mx+radius,my)-(mx+radius,my+20)
  111.     LINE (mx,my)-(mx+radius,my)
  112.     FOR i%=1 TO n%
  113.           w2=w1+2*pi*value(i%)/sum
  114.           CIRCLE (mx,my),radius,1,-w1,-w2 ,f
  115.           'Color Segment
  116.           x=COS(w1+(w2-w1)/2)*radius/2
  117.           y=-f*SIN(w1+(w2-w1)/2)*radius/2
  118.           PAINT STEP(x,y),colr(i%),1
  119.           IF w2>pi THEN
  120.                 'Draw Side Line
  121.                 x=COS(w2)*radius
  122.                 y=-f*SIN(w2)*radius
  123.                 LINE (mx+x,my+y)-(mx+x,my+y+20) 
  124.                 'Color Side
  125.                 IF w2-.1>pi THEN
  126.                   x=COS(w2-.1)*radius
  127.                   y=-f*SIN(w2-.1)*radius
  128.                   PAINT (mx+x,my+y+18),colr(i%),1
  129.                 END IF
  130.           END IF
  131.           w1=w2
  132.     NEXT i%
  133.     ERASE colr
  134. END SUB
  135.  
  136.